# -*- coding: utf-8 -*-
import frappe
from frappe.model.document import Document
from frappe.utils import flt, cint, nowdate, nowtime, get_datetime, format_datetime

class ScrapPickupListItem(Document):
    """
    余料待取清单子表控制器
    处理余料待取清单子表的数据验证和业务逻辑
    """
    
    def validate(self):
        """验证数据有效性"""
        self.validate_scrap_material()
        self.validate_location()
        self.validate_quantity()
        self.fetch_scrap_material_details()
    
    def validate_scrap_material(self):
        """验证余料有效性"""
        if not self.scrap_material:
            frappe.throw("请选择余料")
        
        # 检查余料是否存在且状态为可用
        scrap_material = frappe.get_doc("Scrap Material", self.scrap_material)
        if scrap_material.status != "可用":
            frappe.throw(f"余料 {scrap_material.scrap_id} 状态为 {scrap_material.status}，无法添加到待取清单")
    
    def validate_location(self):
        """验证库位有效性"""
        if not self.location:
            frappe.throw("请选择库位")
        
        # 检查库位是否存在
        location = frappe.get_doc("Scrap Location", self.location)
        
        # 检查库位状态
        if location.status != "启用":
            frappe.throw(f"库位 {location.location_name} 状态为 {location.status}，无法使用")
        
        # 检查余料是否在该库位
        scrap_material = frappe.get_doc("Scrap Material", self.scrap_material)
        if scrap_material.location != self.location:
            frappe.throw(f"余料 {scrap_material.scrap_id} 不在库位 {location.location_name} 中")
    
    def validate_quantity(self):
        """验证数量有效性"""
        if not self.quantity or flt(self.quantity) <= 0:
            frappe.throw("数量必须大于0")
        
        # 获取余料的可用数量
        scrap_material = frappe.get_doc("Scrap Material", self.scrap_material)
        available_quantity = flt(scrap_material.quantity)
        
        if flt(self.quantity) > available_quantity:
            frappe.throw(f"数量 {self.quantity} 超过可用数量 {available_quantity}")
    
    def fetch_scrap_material_details(self):
        """获取余料详细信息"""
        if not self.scrap_material:
            return
        
        scrap_material = frappe.get_doc("Scrap Material", self.scrap_material)
        
        # 设置只读字段
        self.material_type = scrap_material.material_type
        self.material_name = scrap_material.material_name
        self.color_pattern = scrap_material.color_pattern
        self.length = scrap_material.length
        self.width = scrap_material.width
        self.thickness = scrap_material.thickness
        self.uom = scrap_material.uom
    
    def on_change(self):
        """子表项变更时更新父表的汇总信息"""
        parent_doc = frappe.get_doc(self.parenttype, self.parentname)
        parent_doc.calculate_total_quantity()
        parent_doc.save(ignore_permissions=True)
    
    def after_remove(self):
        """子表项删除后更新父表的汇总信息"""
        parent_doc = frappe.get_doc(self.parenttype, self.parentname)
        parent_doc.calculate_total_quantity()
        parent_doc.save(ignore_permissions=True)
    
    def mark_as_picked(self, picked_quantity=None):
        """标记为已取"""
        if not picked_quantity:
            picked_quantity = self.quantity
        
        if flt(picked_quantity) <= 0:
            frappe.throw("已取数量必须大于0")
        
        if flt(picked_quantity) > flt(self.quantity):
            frappe.throw(f"已取数量 {picked_quantity} 超过需取数量 {self.quantity}")
        
        # 更新已取数量和状态
        self.picked_quantity = picked_quantity
        self.status = "已取"
        self.save()
        
        return True
    
    def reset_to_pending(self):
        """重置为待取状态"""
        self.picked_quantity = 0
        self.status = "待取"
        self.save()
        
        return True